﻿using System.IO;
using ECM7.NantContrib.Types;
using NAnt.Core.Attributes;

namespace ECM7.NantContrib.Tasks.Wix
{
	[TaskName("wix-build-file-list")]
	public class WixBuildFileListTask : BaseTask
	{
		public WixBuildFileListTask()
		{
			Folders = new WixFolderElementCollection();
		}

		[TaskAttribute("directoryRef")]
		public string DirectoryRef { get; set; }

		[TaskAttribute("filesResultPath")]
		public FileInfo FilesResultPath { get; set; }

		[TaskAttribute("foldersResultPath")]
		public FileInfo FoldersResultPath { get; set; }

		[BuildElementCollection("folders", "add", ElementType = typeof(WixFolderElement), Required = true)]
		public WixFolderElementCollection Folders { get; set; }

		protected override void ExecuteTask()
		{
			string filesResultPath = FilesResultPath == null ? "Files.wxs" : FilesResultPath.FullName;
			string foldersResultPath = FoldersResultPath == null ? "Folders.wxs" : FoldersResultPath.FullName;
			string directoryRef = string.IsNullOrWhiteSpace(DirectoryRef) ? "INSTALLFOLDER" : DirectoryRef.Trim();

			var builder = new FileListBuilder();
			builder.Execute(filesResultPath, foldersResultPath, directoryRef, Folders);
		}
	}
}
